
<cfcomponent extends="mxunit.framework.TestCase">

<cffunction name="setup" hint="Inspects the ""beansXml"" and ""beans"" attributes on this instance to create a bean factory (if not present in request) and wire in dependencies.">
	<cfset var md = getMetaData( this ) />
	<cfset var bfStruct = "" />
	<cfset var bf = "" />
	
	<cfset super.setup() />
	
	<cfif structKeyExists( request, "_coldspringBeanFactoryForTests" )>
		<cfset bfStruct = request._coldspringBeanFactoryForTests />
	<cfelse>
		<cfset bfStruct = { 
			beanFactory = createObject("component", "coldspring.beans.DefaultXmlBeanFactory" ).init(),
			loadedXml = {}
		} />
		<cfset request._coldspringBeanFactoryForTests = bfStruct />
		<cfset application.beanFactory = bfStruct.beanFactory />
	</cfif>
	
	<cfset variables.beanFactory = bfStruct.beanFactory />
	
	<cfset loadBeanXml( bfStruct ) />
	
	<cfif not structKeyExists( variables, "beans" )>
		<cfset variables.beans = {} />
	</cfif>
	
	<cfset loadBeans( bfStruct ) />
</cffunction>

<cffunction name="loadBeanXml" access="private">
	<cfargument name="bfStruct" required="true" />
	<cfargument name="md" />
	
	<cfset var beanPath = "" />

	<cfif not structKeyExists( arguments, "md" ) >
		<cfset arguments.md = getMetadata( this ) />
	</cfif>
	
	<cfif structKeyExists( md, "beansXml" )>
		<cfloop list="#md.beansXml#" index="beanPath">
			<cfif not structKeyExists( bfStruct.loadedXml, expandPath( beanPath ) )>
				<cfset bfStruct.loadedXml[ expandPath( beanPath ) ] = true />
				<cfset bfStruct.beanFactory.loadBeans( expandPath( beanPath ) ) />
				<cflog text="Loading test beans: #beanPath#" />
			</cfif>
		</cfloop>
	</cfif>
	
	<cfif structKeyExists( md, "extends")>
		<cfset loadBeanXml( bfStruct, md.extends ) />
	</cfif>
</cffunction>

<cffunction name="loadBeans" access="private">
	<cfargument name="bfStruct" required="true" />
	<cfargument name="md" />

	<cfset var beanId = "" />

	<cfif not structKeyExists( arguments, "md" ) >
		<cfset arguments.md = getMetadata( this ) />
	</cfif>
	
	<cfif structKeyExists( md, "beans" )>
		<cfloop list="#md.beans#" index="beanId">
			<cfset variables.beans[ beanId ] = bfStruct.beanFactory.getBean( beanId ) />
		</cfloop>
	</cfif>

	<cfif structKeyExists( md, "extends")>
		<cfset loadBeans( bfStruct, md.extends ) />
	</cfif>
</cffunction>

</cfcomponent>